﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class CT_HoaDonBanVeDAO
    {
        public List<CT_HoaDonBanVeDTO> LayChiTietHoaBanVeTheoMa(int mahd)
        {
            List<CT_HoaDonBanVeDTO> listCTHDBV = new List<CT_HoaDonBanVeDTO>();
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                String sql = "Select MaHD, ct.MaLoai, Soluong, Ten, Gia from CTHoaDonbanVe ct, LoaiVe lv Where MaHD = " + mahd + " and ct.MaLoai = lv.MaLoai";

                conn = DataProvider.ConnectDB();
                SqlCommand cm = new SqlCommand(sql, conn);
                reader = cm.ExecuteReader();
                while (reader.Read())
                {
                    CT_HoaDonBanVeDTO hdbvDTO = new CT_HoaDonBanVeDTO();
                    hdbvDTO.MaHD = reader.GetInt32(0);
                    hdbvDTO.MaLoai = reader.GetInt32(1);
                    hdbvDTO.SoLuong = reader.GetInt32(2);
                    hdbvDTO.TenLoai = reader.GetString(3);
                    hdbvDTO.Gia = Convert.ToDouble(reader.GetDecimal(4));

                    listCTHDBV.Add(hdbvDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return listCTHDBV;
        }

        public String ThemChiTietHoaDonbanVe(CT_HoaDonBanVeDTO cthdbvDTO)
        {
            String mess = "";
            SqlConnection conn = null;
            try
            {
                conn = DataProvider.ConnectDB();
                String sql = "INSERT INTO CTHoaDonBanVe(MaHD,MaLoai,SoLuong)";
                sql += "VALUES(@MaHD,@MaLoai,@SoLuong)";
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                SqlParameter para;
                para = cmd.Parameters.Add("@MaHD", SqlDbType.Int);
                para.Value = cthdbvDTO.MaHD;
                para = cmd.Parameters.Add("@MaLoai", SqlDbType.Int);
                para.Value = cthdbvDTO.MaLoai;
                para = cmd.Parameters.Add("@SoLuong", SqlDbType.Int);
                para.Value = cthdbvDTO.SoLuong;

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                mess = "Lỗi thêm chi tiết hóa đơn";
                String e = ex.ToString();
            }
            finally
            {
                conn.Close();
            }
            return mess;
        }
    }
}
